//
//  SN_QuestionAlert.m
//  Shiyun_Newspaper
//
//  Created by zz on 17/6/21.
//  Copyright © 2017年 ShiYun. All rights reserved.
//

#import "SN_QuestionAlert.h"

#import "SN_Qustion_Year_Model.h"
#import "SN_AlertModel.h"

@interface SN_QuestionAlert() <UITableViewDelegate,UITableViewDataSource,UIGestureRecognizerDelegate>

/**标题*/
@property (nonatomic, strong) UILabel *titleLabel;
/**学年度*/
@property (nonatomic, strong) UILabel *yearLabel;
/**期数*/
@property (nonatomic, strong) UILabel *editionLabel;

/**学年度按钮*/
@property (nonatomic, strong) UIButton *yearButton;
/**期数按钮*/
@property (nonatomic, strong) UIButton *editionButton;

/**取消按钮*/
@property (nonatomic, strong) UIButton *cancelButton;
/**确定按钮*/
@property (nonatomic, strong) UIButton *confirmButton;

/**学年度箭头*/
@property (nonatomic, strong) UIImageView *yearBtnImageView;
/**期数箭头*/
@property (nonatomic, strong) UIImageView *editionBtnImageView;

/**年份tableview*/
@property (nonatomic, strong) UITableView *yearTableView;
/**期数tableview*/
@property (nonatomic, strong) UITableView *editionTableView;

/**选择的年份*/
@property (nonatomic, copy) NSString *yearStr;
/**选择的期数*/
@property (nonatomic, copy) NSString *editionTypeStr;

/**选择年份列表的序列*/
@property (nonatomic, assign) NSUInteger selectYearNum;

@end

@implementation SN_QuestionAlert

- (instancetype)initWithSuperView:(UIView *)superView andTitle:(NSString *)titleStr {
    
    if (self = [super init]) {
        
        _selectYearNum = 0;
        
        _titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 16, 270, 17)];
        _titleLabel.textColor = [UIColor colorWithHexString:@"#3b3b3b"];
        _titleLabel.font = [UIFont boldSystemFontOfSize:17];
        _titleLabel.text = titleStr;
        _titleLabel.textAlignment = NSTextAlignmentCenter;
        [self addSubview:_titleLabel];
        
        _yearLabel = [[UILabel alloc] initWithFrame:CGRectMake(35, 51, 59, 16)];
        _yearLabel.font = [UIFont systemFontOfSize:16];
        _yearLabel.textColor = [UIColor colorWithHexString:@"#3b3b3b"];
        _yearLabel.text = @"学年度";
        [self addSubview:_yearLabel];
        
        _yearButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _yearButton.frame = CGRectMake(CGRectGetMaxX(_yearLabel.frame), 0, 139, 29);
        _yearButton.centerY = _yearLabel.centerY;
        _yearButton.layer.borderWidth = 0.5;
        _yearButton.layer.cornerRadius = 3;
        _yearButton.layer.borderColor = [UIColor colorWithHexString:@"#dedede"].CGColor;
        [_yearButton setTitle:@"2016-2017" forState:UIControlStateNormal];
        _yearButton.titleLabel.font = [UIFont systemFontOfSize:15];
        [_yearButton setTitleColor:[UIColor colorWithHexString:@"#3b3b3b"] forState:UIControlStateNormal];
        [_yearButton setTitleEdgeInsets:UIEdgeInsetsMake(0, -_yearButton.imageView.image.size.width, 0, _yearButton.imageView.image.size.width)];
        _yearBtnImageView = [[UIImageView alloc] initWithFrame:CGRectMake(40, 0, 30, 20)];
        _yearBtnImageView.image = [UIImage imageNamed:@"展开"];
        _yearBtnImageView.frame = CGRectMake(_yearButton.width - 10 - 14, 12, 13,6);
        
        [_yearButton addTarget:self action:@selector(selectYear:) forControlEvents:UIControlEventTouchUpInside];
        [_yearButton addSubview:_yearBtnImageView];
        [self addSubview:_yearButton];
        
        _editionLabel = [[UILabel alloc] initWithFrame:CGRectMake(35, CGRectGetMaxY(_yearLabel.frame) + 26, 59, 16)];
        _editionLabel.font = [UIFont systemFontOfSize:16];
        _editionLabel.textColor = [UIColor colorWithHexString:@"#3b3b3b"];
        _editionLabel.text = @"期 数";
        [self addSubview:_editionLabel];
        
        _editionButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _editionButton.frame = CGRectMake(CGRectGetMaxX(_editionLabel.frame), 0, 139, 29);
        _editionButton.centerY = _editionLabel.centerY;
        _editionButton.layer.borderWidth = 0.5;
        _editionButton.layer.cornerRadius = 3;
        _editionButton.layer.borderColor = [UIColor colorWithHexString:@"#dedede"].CGColor;
        [_editionButton setTitle:@"第8期" forState:UIControlStateNormal];
        _editionButton.titleLabel.font = [UIFont systemFontOfSize:15];
        [_editionButton setTitleColor:[UIColor colorWithHexString:@"#3b3b3b"] forState:UIControlStateNormal];

        [_editionButton setTitleEdgeInsets:UIEdgeInsetsMake(0, -_editionButton.imageView.image.size.width, 0, _editionButton.imageView.image.size.width)];

        _editionBtnImageView = [[UIImageView alloc] initWithFrame:CGRectMake(40, 0, 30, 20)];
        _editionBtnImageView.image = [UIImage imageNamed:@"展开"];
        _editionBtnImageView.frame = CGRectMake(_editionButton.width - 10 - 14, 12, 13,6);
        [_editionButton addTarget:self action:@selector(selectEditionType:) forControlEvents:UIControlEventTouchUpInside];
        [_editionButton addSubview:_editionBtnImageView];

        [self addSubview:_editionButton];
        
        UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(_editionButton.frame) + 16, 270, 0.5)];
        lineView.backgroundColor = [UIColor colorWithHexString:@"#dedede"];
        [self addSubview:lineView];
        
        _cancelButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_cancelButton setTitle:@"取消" forState:UIControlStateNormal];
        [_cancelButton setTitleColor:[UIColor colorWithHexString:@"#3b3b3b"] forState:UIControlStateNormal];
        [_cancelButton addTarget:self action:@selector(cancelAction) forControlEvents:UIControlEventTouchUpInside];
        _cancelButton.titleLabel.font = [UIFont systemFontOfSize:16];
        _cancelButton.frame = CGRectMake(0, CGRectGetMaxY(lineView.frame), 270 / 2 -0.5, 44);
        [self addSubview:_cancelButton];
        
        UIView *divView = [[UIView alloc] initWithFrame:CGRectMake(CGRectGetMaxX(_cancelButton.frame), CGRectGetMaxY(lineView.frame) + 10, 0.5, 24)];
        divView.backgroundColor = [UIColor colorWithHexString:@"#dedede"];
        [self addSubview:divView];
        
        _confirmButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_confirmButton setTitle:@"确定" forState:UIControlStateNormal];
        [_confirmButton setTitleColor:[UIColor colorWithHexString:@"#fb524a"] forState:UIControlStateNormal];
        _confirmButton.titleLabel.font = [UIFont systemFontOfSize:16];
        _confirmButton.frame = CGRectMake(CGRectGetMaxX(divView.frame), CGRectGetMaxY(lineView.frame), 270 / 2 - 0.5, 44);
        [_confirmButton addTarget:self action:@selector(confirmAction) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:_confirmButton];
        
        
        
        self.width = 270;
        self.height = 180;
        self.center = superView.center;
        self.backgroundColor = [UIColor whiteColor];
        self.layer.cornerRadius = 10;
        [superView addSubview:self];

        //给superView添加点击手势收回tableView
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction)];
        tap.delegate = self;
        [superView addGestureRecognizer:tap];
        
        //创建两个显示数据的tableview
        CGRect rect = [self convertRect:_yearButton.frame toView:superView];
        _yearTableView = [[UITableView alloc] initWithFrame:CGRectMake(rect.origin.x, CGRectGetMaxY(rect), rect.size.width, 156) style:UITableViewStylePlain];
        _yearTableView.delegate = self;
        _yearTableView.dataSource = self;
        _yearTableView.hidden = YES;
        _yearTableView.separatorInset = UIEdgeInsetsMake(0, 0, 0, 15);
        _yearTableView.tableFooterView = [[UIView alloc] init];
        [superView addSubview:_yearTableView];
        [superView bringSubviewToFront:_yearTableView];
        
        CGRect rect1 = [self convertRect:_editionButton.frame toView:superView];
        _editionTableView = [[UITableView alloc] initWithFrame:CGRectMake(rect1.origin.x, CGRectGetMaxY(rect1), rect1.size.width, 150) style:UITableViewStylePlain];
        _editionTableView.delegate = self;
        _editionTableView.dataSource = self;
        _editionTableView.hidden = YES;
        _editionTableView.separatorInset = UIEdgeInsetsMake(0, 0, 0, 15);
        _editionTableView.tableFooterView = [[UIView alloc] init];
        [superView addSubview:_editionTableView];
        [superView bringSubviewToFront:_editionTableView];
        
        
    }
    
    return self;
}


#pragma mark -Actions

//选择学年度
- (void)selectYear:(UIButton *)sender {

    if (sender.selected == NO) {
        
        _yearBtnImageView.image = [UIImage imageNamed:@"收起"];
        _yearTableView.hidden = NO;
        
        if (_editionButton.selected == YES) {
            
            [self selectEditionType:_editionButton];
        }
        
    }else if (sender.selected == YES) {
        _yearBtnImageView.image = [UIImage imageNamed:@"展开"];
        _yearTableView.hidden = YES;
    }
    
    sender.selected = !sender.selected;
}

//选择期数
- (void)selectEditionType:(UIButton *)sender {

    if (sender.selected == NO) {
        
        _editionBtnImageView.image = [UIImage imageNamed:@"收起"];
        _editionTableView.hidden = NO;
        
        if (_yearButton.selected == YES) {
            
            [self selectYear:_yearButton];
        }
    }else if (sender.selected == YES) {
        _editionBtnImageView.image = [UIImage imageNamed:@"展开"];
        _editionTableView.hidden = YES;
    }
    
    sender.selected = !sender.selected;
}

//取消
- (void)cancelAction {
    
    [self.superview removeFromSuperview];
}

//确定
- (void)confirmAction {
    
    if (self.delegate && [self.delegate respondsToSelector:@selector(confirmWithYear:andEditionType:)]) {
        
        if (_yearStr.length == 0) {
            [self makeToast:@"请选择学年度"];
            return;
        }
        
        if (_editionTypeStr.length == 0) {
            [self makeToast:@"请选择期数"];
            return;
        }
        
        [self.delegate confirmWithYear:_yearStr andEditionType:_editionTypeStr];
        [self.superview removeFromSuperview];
    }
}

//收回tableview
- (void)tapAction {
    
    if (self.yearButton.selected == YES) {
        
        [self selectYear:self.yearButton];
    }
    
    if (self.editionButton.selected == YES) {
        
        [self selectEditionType:self.editionButton];
    }
}



#pragma mark -Setter
- (void)setDataArr:(NSMutableArray *)dataArr {
    
    _dataArr = dataArr;
    
    //改变button上文字
    SN_AlertModel *model = [self.dataArr objectAtIndex:0];
    [_yearButton setTitle:model.year forState:UIControlStateNormal];
    _yearStr = model.year;
    
    SN_Qustion_Year_Model *yearModel = [model.phaselist objectAtIndex:0];
    [_editionButton setTitle:yearModel.phase forState:UIControlStateNormal];
    _editionTypeStr = yearModel.phase;
    
    //刷新tableview
    [self.yearTableView reloadData];
    [self.editionTableView reloadData];
}

#pragma mark -UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    if (tableView == self.yearTableView) {
     
        return _dataArr.count;
        
    }else {
    
        SN_AlertModel *model = [_dataArr objectAtIndex:_selectYearNum];
        return model.phaselist.count;

    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *cellId1 = @"cellid1";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId1];
    
    if (!cell) {
        
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId1];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.textLabel.textAlignment = NSTextAlignmentCenter;
        cell.textLabel.textColor = [UIColor colorWithHexString:@"#3b3b3b"];
        cell.textLabel.font = [UIFont systemFontOfSize:15];
    }
    
    if (tableView == self.yearTableView) {
        
        SN_AlertModel *model = [self.dataArr objectAtIndex:indexPath.row];
        cell.textLabel.text = model.year;
    }else {
        
        SN_AlertModel *alertModel = [self.dataArr objectAtIndex:_selectYearNum];
        SN_Qustion_Year_Model *model = [alertModel.phaselist objectAtIndex:indexPath.row];
        cell.textLabel.text = model.phase;
    }
    
    return cell;
}


#pragma mark -UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (tableView == _yearTableView) {
        
        [self selectYear:_yearButton];
        
//        if (_selectYearNum == indexPath.row) { //与上次选择的年份一样
//            
//            return;
//        }
        
        
        _selectYearNum = indexPath.row;
        
        [self.editionTableView reloadData];
        
        SN_AlertModel *model = [self.dataArr objectAtIndex:_selectYearNum];
        
        [_yearButton setTitle:model.year forState:UIControlStateNormal];
        
        SN_Qustion_Year_Model *yearModel = [model.phaselist objectAtIndex:0];
        [_editionButton setTitle:yearModel.phase forState:UIControlStateNormal];
        
        //选择的年份
        _yearStr = model.year;
        
    }else {
        
        [self selectEditionType:_editionButton];
    
        SN_AlertModel *model = [self.dataArr objectAtIndex:_selectYearNum];
        SN_Qustion_Year_Model *yearModel = [model.phaselist objectAtIndex:indexPath.row];
        
        [_editionButton setTitle:yearModel.phase forState:UIControlStateNormal];
        
        //选择的期数
        _editionTypeStr = yearModel.phase;
    }
}

#pragma mark -UIGestureDelegate
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
    
    CGPoint point = [touch locationInView:self.superview];
    
    if (CGRectContainsPoint(self.yearTableView.frame, point)) {
        
        return NO;
    }
    
    return YES;
}

@end
